"""
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    created by lane.chang on '28/05/2024'
    comment: 毫米微波雷达相关
"""
from fastapi import APIRouter, Request, Body

from config import Config
from project.lib.decorator import api_common, exec_duration
from project.core.function import json_response
from project.model.basic import User
from project.service.agents.agent import Agent
from project.service.agents.radar import RadarService
from project.model.basic import Room


hr_router = APIRouter(prefix='', tags=['会议室咨询'])

@hr_router.post('/radar/user/memories/init', summary='清空会议室记忆')
@api_common(doc_replace=(('{0}', Config.HTTP_DOC_FIX),))
@exec_duration(add_return=True)
async def init_user_memories(request: Request, dev_id: str = Body(..., title='设备id', embed=True)):
    """
    **desc**: 清空用户记忆

    **url**: http(s)://{0}.udicaria.com/radar/user/memories/init

    **method**: POST

    **params**:

        <body>:
            dev_id: 设备id

    **return**:json数据结构::

        {
            code: 0(成功)/非0(失败)
            message: 成功返回success, 否则返回error信息
            data:
            exec_time: 接口后端处理时长
        }
    """
    user = User(dev_id=dev_id)
    agent = Agent(user, user_message='', memories_key='room_enquire')
    agent.init_user_memories()

    return json_response()


@hr_router.post('/radar/user/memories/add', summary='添加会议室记忆')
@api_common(doc_replace=(('{0}', Config.HTTP_DOC_FIX),))
@exec_duration(add_return=True)
async def add_user_memories(request: Request,
                            dev_id: str = Body(..., title='设备id'),
                            user_message: str = Body('', title='用户话术'),
                            agent_message:  str = Body('', title='agent话术'),):
    """
    **desc**: 添加面试记忆

    **url**: http(s)://{0}.udicaria.com/radar/user/memories/add

    **method**: POST

    **params**:

        <body>:
            dev_id: 设备id
            user_message: 用户话术
            agent_message: agent话术

    **return**:json数据结构::

        {
            code: 0(成功)/非0(失败)
            message: 成功返回success, 否则返回error信息
            data:
            exec_time: 接口后端处理时长
        }
    """
    user = User(dev_id=dev_id)
    agent = Agent(user, user_message='', memories_key='room_enquire')
    if user_message:
        agent.add_user_memories()
        agent.add_user_memories(agent.format(user_message))
    if agent_message:
        agent.add_user_memories(agent.format(agent_message, _type='out'))

    return json_response()


@hr_router.post('/radar/enquire', summary='会议室问询')
@api_common(doc_replace=(('{0}', Config.HTTP_DOC_FIX),))
@exec_duration(add_return=True)
async def do_enquire(request: Request,
                     dev_id: str = Body(..., title='设备id'),
                     room_info: list[Room] = Body(..., title='房间信息列表'),
                     user_message: str = Body('', title='用户话术')):
    """
    **desc**: 会议室闻讯

    **url**: http(s)://{0}.udicaria.com/radar/enquire

    **method**: POST

    **params**:

        <body>:
            dev_id: 设备id
            room_info: 房间信息
            user_message: 用户话术

    **return**:json数据结构::

        {
            code: 0(成功)/非0(失败)
            message: 成功返回success, 否则返回error信息
            data:
            exec_time: 接口后端处理时长
        }
    """
    result = await RadarService.enquire(dev_id, room_info, user_message)

    return json_response(data=result)
